#include "nstore.h"
#include "ui_nstore.h"

nstore::nstore(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::nstore)
{
    ui->setupUi(this);
    connect(ui->cancel, &QPushButton::clicked, this, &nstore::cc);
    connect(ui->YesButton, &QPushButton::clicked, this, &nstore::save);
}

nstore::~nstore()
{
    delete ui;
}

void nstore::cc() {
    this->close();
}

void nstore::save() {
    QString StoreID = ui->StoreID->text();
    QString Sname = ui->Sname->text();
    QString BookID = ui->BookID->text();

    // 验证输入不为空
    if (StoreID.isEmpty() || Sname.isEmpty() || BookID.isEmpty()) {
        QMessageBox::warning(this, "错误", "请填写所有必要信息！");
        return;
    }

    // 执行SQL插入
    QSqlDatabase db = QSqlDatabase::database("qt_sql_default_connection");
    if (!db.isOpen()) {
        QMessageBox::warning(this, "错误", "数据库未连接！");
        return;
    }

    QSqlQuery query(db);
    query.prepare("INSERT INTO StoreList (StoreID, Sname, BookID) "
                  "VALUES (?, ?, ?)");
    query.addBindValue(StoreID);
    query.addBindValue(Sname);
    query.addBindValue(BookID);

    if (query.exec()) {
        QMessageBox::information(this, "成功", "数据添加成功！");
        this->close();  // 关闭窗口
    } else {
        QMessageBox::critical(this, "错误",
                              QString("数据添加失败"));
    }
}
